package com.msmall.community.bbs.preparer;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.model.Board;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Sight;
import com.msmall.community.common.service.SightManager;

public class SightPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(SightPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Long boardId = null;
			try{
				boardId = (Long) context.getAttribute("boardId").getValue();
			}catch(Exception e){
				
			}
			CascadeCollection<Board> boardCS = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
			Board board = boardCS.getAllDatas().get(boardId).getCurrent();
			request.setAttribute("bbsBoard", board);
			
			SightManager sm = (SightManager) ApplicationManager.appContext.getBean("sightManager");
			Sight sight = sm.loadSightByBoard(boardId);
			request.setAttribute("bbsSight", sight);
		}catch(Exception e){
			log.debug(e);
		}
	}

}
